package com.sas.repositoryImpl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sas.domain.Article;
import com.sas.domain.Flow;
import com.sas.domain.Vendor;
import com.sas.repository.FlowRepository;

@Repository
public class HibernateFlowRepository extends HibernateGenericRepository<Flow> 
		implements FlowRepository {

	@Autowired
	public HibernateFlowRepository (SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Flow> findByArticle(Article article, String property,
			boolean ascending) {
		Session session = getSession();
		String asc = ascending? " asc" : " desc";
		
		if (article == null) {
			return null;
		}
		
		Query query = session.createQuery("select v from Flow v inner join v.article a " +
				"where a = ? " +
				"order by " + property + asc);
		query.setParameter(0, article);
		                                      
		
		List<Flow> result = (List<Flow>)query.list();
		return result;
	}


	@Override
	public List<Flow> findByArticleField(Vendor selectedVendor,
			String property, boolean ascending) {
		// TODO Auto-generated method stub
		return null;
	}

}
